home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / CStream.cp < prev    next >
Encoding:
Text File  |  1995-12-06  |  4.8 KB  |  316 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    CStream.cp - I/O stream classes BBEdit extensions.
  4.   *    Copyright © 1995 by Christopher E. Hyde.  All rights reserved.
  5.   *
  6.   ***/
  7.  
  8. #include "Exceptions.h"
  9. #include "BBEdit.h"
  10. #include "CStream.h"
  11.  
  12.  
  13. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  14. // Class CStream
  15. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  16.  
  17. #if 0
  18. CStream::CStream (void)
  19. {
  20.     fHandle = nil;
  21. }
  22. #endif
  23.  
  24.  
  25. void
  26. CStream::Open (Handle aHandle, bool direction, bool ownsHandle)
  27. {
  28.     FailNil(fHandle = aHandle);
  29.     fDirection  = direction;
  30.     fOwnsHandle = ownsHandle;
  31.  
  32.     HLockHi(aHandle);        
  33.     fBase   = (uchar*) *aHandle;
  34.     fLength = InlineGetHandleSize(aHandle);
  35.     fOffset = 0;
  36. }
  37.  
  38.  
  39. void
  40. CStream::SetLength (UInt32 length)
  41. {
  42.     HUnlock(fHandle);
  43.     SetHandleSize(fHandle, length);
  44.     FailMemError();
  45.     HLockHi(fHandle);
  46.  
  47.     fBase   = (uchar*) *fHandle;
  48.     fLength = length;
  49.     if (fOffset > length)
  50.         fOffset = length;
  51. }
  52.  
  53.  
  54. Handle
  55. CStream::GetHandle (void)
  56. {
  57.     SetLength(fOffset);
  58.     return fHandle;
  59. }
  60.  
  61.  
  62. void
  63. CStream::Flush (void)
  64. {
  65.     if (fOffset > 0) {
  66.         BBEdit->Insert(Ptr(fBase), fOffset);
  67.         fOffset = 0;
  68.     }
  69. }
  70.  
  71.  
  72. void
  73. CStream::Close (void)
  74. {
  75. #if 0
  76.     if (fHandle) {
  77.         if (IsOutput()) {    // If it's and output stream then it owns the Handle
  78.             Flush();
  79.             DisposeHandle(fHandle);
  80.             fHandle = nil;
  81.         } else
  82.             HUnlock(fHandle);
  83.     }
  84. #else
  85.     if (fHandle) {
  86.         if (IsOutput())        // If it's an output stream then flush it
  87.             Flush();
  88.         if (OwnsHandle())
  89.             DisposeHandle(fHandle);
  90.         else
  91.             HUnlock(fHandle);
  92.         fHandle = nil;
  93.         fLength = 0;
  94.     }
  95. #endif
  96. }
  97.  
  98.  
  99. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  100. // Class CIStream
  101. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  102.  
  103. #if 0
  104. CIStream::End (void)
  105. {
  106.     if (fHandle)
  107.         HUnlock(fHandle);
  108. }
  109. #endif
  110.  
  111.  
  112. void
  113. CIStream::Open (WindowPtr window)
  114. {
  115.     CStream::Open(BBEdit->GetWindowContents(window), kInputStream, !kOwnsHandle);
  116. }
  117.  
  118.  
  119. // Initialise the input stream to the current selection
  120. void
  121. CIStream::Open (WindowPtr window, long selStart, long selEnd)
  122. {
  123.     Open(window);
  124.     fOffset = selStart;
  125.     fLength = selEnd;
  126. }
  127.  
  128.  
  129. int
  130. CIStream::Get (void)
  131. {
  132.     return (fOffset < fLength) ? fBase[fOffset++] : EOF;
  133. }
  134.  
  135.  
  136. // Returns a line in DST if the line length < SIZE, otherwise fails
  137. // If this is the last line then a CR is appended!
  138.  
  139. void
  140. CIStream::Get (char* dst, int size)
  141. {
  142. #if 1
  143.     char c;
  144.     --size;        // Subtract 1 for the CR
  145.     while ((c = Get()) != EOF && c != '\x0D' && --size > 0)
  146.         *dst++ = c;
  147.     *dst++ = '\x0D';
  148.     *dst = '\0';
  149. #elif 0
  150.     while (fOffset < fLength && --size > 0) {
  151.         char c = fBase[fOffset++];
  152.         *dst++ = c;
  153.         if (c == '\x0D')
  154.             break;
  155.     }
  156.     *dst = '\0';
  157. //    return size;
  158.  
  159. #else
  160.  
  161.     const char* src = &fBase[fOffset];
  162.     if (size > fLength - fOffset)
  163.         size = fLength - fOffset;
  164.     const char* max = &src[size - 2];
  165.  
  166.     while (src < max) {
  167.         char c = src++;
  168.         *dst++ = c;
  169.         if (c == '\x0D')
  170.             goto gotCR;
  171.     }
  172.     if (src < &fBase[fLength])
  173.         Fail(errEOF);
  174.     *dst++ = '\x0D';
  175. gotCR:
  176.     *dst = '\0';
  177.     fOffset = src - fBase;
  178. #endif
  179. }
  180.  
  181.  
  182. int
  183. CIStream::CurChar (void) const
  184. {
  185.     return (fOffset < fLength) ? fBase[fOffset] : EOF;
  186. }
  187.  
  188.  
  189. #if 0
  190. bool
  191. CIStream::IsEOF (void) const
  192. {
  193.     return fOffset < fLength;
  194. }
  195.  
  196.  
  197. void
  198. CIStream::Close (void)
  199. {
  200.     if (fHandle)
  201.         HUnlock(fHandle);
  202. }
  203. #endif
  204.  
  205.  
  206. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  207. // Class COStream
  208. // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  209.  
  210. #if 0
  211. COStream::~COStream (void)
  212. {
  213.     if (fHandle)
  214.         DisposeHandle(fHandle);
  215. }
  216. #endif
  217.  
  218.  
  219. void
  220. COStream::Open (bool autoFlush, UInt32 length)
  221. {
  222.     fAutoFlush = autoFlush;
  223.     CStream::Open(NewHandle(length), kOutputStream, kOwnsHandle);
  224. }
  225.  
  226.  
  227. void
  228. COStream::Put (char ch)
  229. {
  230.     if (fOffset >= fLength)
  231.         Flush();
  232.  
  233.     fBase[fOffset++] = ch;
  234.  
  235.     if (fAutoFlush)
  236.         Flush();
  237. }
  238.  
  239.  
  240. void
  241. COStream::Put (const char* ptr, UInt32 length)
  242. {
  243. #if 0
  244.     if (fOffset + length > fLength)
  245.         SetLength(fLength + Max(kGrowStream, length));
  246.  
  247.     BlockMoveData(ptr, fBase + fOffset, length);
  248.     fOffset += length;
  249. #else
  250.     if (fOffset + length > fLength)
  251.         Flush();
  252.  
  253.     for (UInt32 i = fLength - fOffset; length > i; i = fLength - fOffset) {
  254.         BlockMoveData(ptr, fBase + fOffset, i);
  255.         fOffset += i;
  256.         ptr += i;
  257.         length -= i;
  258.         Flush();
  259.     }
  260.  
  261.     BlockMoveData(ptr, fBase + fOffset, length);
  262.     fOffset += length;
  263. #endif
  264.     if (fAutoFlush)
  265.         Flush();
  266. }
  267.  
  268.  
  269. void
  270. COStream::Put (ConstStr255Param aString)
  271. {
  272.     Put((const char*) &aString[1], StrLength(aString));
  273. }
  274.  
  275.  
  276. void
  277. COStream::Put (const char* str)
  278. {
  279.     Put(str, strlen(str));
  280. }
  281.  
  282.  
  283. void
  284. COStream::NewLine (void)
  285. {
  286.     Put('\x0D');
  287. }
  288.  
  289.  
  290. #if 0
  291. Handle
  292. COStream::GetHandle (void)
  293. {
  294.     SetLength(fOffset);
  295.     return fHandle;
  296. }
  297.  
  298.  
  299. void
  300. COStream::Flush (void)
  301. {
  302.     BBEdit->Paste(GetHandle());
  303.     fOffset = 0;
  304. }
  305.  
  306.  
  307. void
  308. COStream::Close (void)
  309. {
  310.     if (fHandle) {
  311.         DisposeHandle(fHandle);
  312.         fHandle = nil;
  313.     }
  314. }
  315. #endif
  316.